package com.xfcycle.zebrabikegp.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.xfcycle.zebrabikegp.R;
import com.xfcycle.zebrabikegp.constant.BluetoothDeviceConstant;
import com.xfcycle.zebrabikegp.constant.WebConstant;
import com.xfcycle.zebrabikegp.util.HandleDataByBLEUtil;

import java.util.HashMap;
import java.util.Map;

public class DebugActivity extends RootActivity {

    private static final String TAG = "DebugActivity";

    private ImageView iv_debug_back;

    private EditText et_debug_test;
    private Button bt_debug_send;
    private Button bt_debug_output;
    private Button bt_debug_light;
    private Button bt_debug_sleep;
    private Button bt_debug_release_alarm;

    private TextView tv_debug_send_instruct;
    private TextView tv_debug_receive_instruct;

    private DebugHandleDataFromBleReceiver debugHandleDataFromBleReceiver;
    private HandleDataByBLEUtil handleDataByBLEUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debug);

        initView();
        initViewListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //注册广播
        debugHandleDataFromBleReceiver = new DebugHandleDataFromBleReceiver();
        registBroadcastReceiverAtActivity(debugHandleDataFromBleReceiver, BluetoothDeviceConstant.GET_DATA_FROM_BLUETOOTH_BROADCAST_RECEIVER_ACTION);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //回收广播接收者
        unregisterReceiver(debugHandleDataFromBleReceiver);
    }

    private void initView() {
        iv_debug_back = (ImageView) findViewById(R.id.iv_debug_back);
        et_debug_test = (EditText) findViewById(R.id.et_debug_test);
        bt_debug_send = (Button) findViewById(R.id.bt_debug_send);
        bt_debug_output = (Button) findViewById(R.id.bt_debug_output);
        bt_debug_light = (Button) findViewById(R.id.bt_debug_light);
        bt_debug_sleep = (Button) findViewById(R.id.bt_debug_sleep);
        bt_debug_release_alarm = (Button) findViewById(R.id.bt_debug_release_alarm);

        tv_debug_send_instruct = (TextView) findViewById(R.id.tv_debug_send_instruct);
        tv_debug_receive_instruct = (TextView) findViewById(R.id.tv_debug_receive_instruct);

        handleDataByBLEUtil = HandleDataByBLEUtil.getInstance(this, -1);
    }

    private void initViewListener() {
        //回退事件监听
        iv_debug_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                closeActivity();
            }
        });

        bt_debug_send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = et_debug_test.getText().toString();
                handleDataByBLEUtil.sendDataToDevice(str);

                tv_debug_send_instruct.setText(str);
                tv_debug_receive_instruct.setText("- - - - -");
            }
        });

        //输出功率
        bt_debug_output.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                handleDataByBLEUtil.sendDataToDevice("s|2,r");
            }
        });

        bt_debug_light.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                handleDataByBLEUtil.sendDataToDevice("s|1,4");
            }
        });

        bt_debug_sleep.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                handleDataByBLEUtil.sendDataToDevice("s|4,r");
            }
        });

        //解除报警
        bt_debug_release_alarm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> params = new HashMap<>();
                params.put(WebConstant.SEND_INSTRUCTION_TO_BIKE_CMD, "2");
                new GetJsonFromWebJob(WebConstant.SEND_INSTRUCTION_TO_BIKE_PATH_ONE + "1987" + WebConstant.SEND_INSTRUCTION_TO_BIKE_PATH_TWO, WebConstant.POST, params, REQUEST_SIGN_LOGOUT, DebugActivity.this).start();
            }
        });
    }

    /***********************************************************************************************/
    /**                                 自定义广播接收者类                                          **/
    /***********************************************************************************************/

    /**
     * 广播接收者类，负责接收从蓝牙发出的数据并对其进行处理
     */
    class DebugHandleDataFromBleReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String result = intent.getStringExtra("result");

            tv_debug_receive_instruct.setText(result);
        }
    }

}
